package com.kamistoat.meimeistore.search.web;

import com.kamistoat.meimeistore.search.vo.SearchParamVo;
import com.kamistoat.meimeistore.search.service.ListSearchService;
import com.kamistoat.meimeistore.search.vo.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class indexSearchController {

    @Autowired
    ListSearchService listSearchService;

    /**
     * 在商城首页点击三级菜单时或搜索按钮时，跳转到搜索页
     * 在搜索页继续搜索，还是该方法响应
     *
     * @param searchParamVo 查询条件Vo
     * @param model         用于向Thymeleaf的html文件交换数据
     * @param request       直接把HTTP请求的URL获取进来
     * @return 页面html文件名
     */
    @GetMapping({"/", "/list&search.html"})
    public String listSearchPage(SearchParamVo searchParamVo, Model model, HttpServletRequest request) {
        // 用HttpServletRequest.getQueryString()可以把URL中从?开始之后的字符串提取出来
        String queryString = request.getQueryString();
        searchParamVo.setQueryString(queryString);
        // 调用服务层方法，根据查询条件Vo到ES中查询数据，并返回封装好的SearchResponseVo类
        SearchResponseVo listSearchResult = listSearchService.search(searchParamVo);
        // 将数据渲染到html中
        model.addAttribute("listSearchResult", listSearchResult);
        return "list&search";
    }
}
